package com.produce.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.produce.common.constant.SessionAttr;
import com.produce.common.persistence.Page;
import com.produce.common.util.ValidateUtils;
import com.produce.system.constans.SysRoleConstants;
import com.produce.system.entity.SysMenu;
import com.produce.system.entity.SysRole;
import com.produce.system.service.SysMenuService;
import com.produce.system.service.SysRoleService;

@Controller
@RequestMapping(value = "sysrole")
public class SysRoleController {
	private Logger logger = Logger.getLogger(SysRoleController.class);
	@Resource
	private SysRoleService sysRoleService;
	@Resource
	private SysMenuService sysMenuService;
	/**
	 * 初始化角色页面
	 */
	@RequestMapping(value = "init")
	public ModelAndView initUser(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) {
		logger.info("初始化角色页面");
		return new ModelAndView(SysRoleConstants.INITROLE,modelMap);
	}
	/**
	 * 角色的列表页面
	 */
	@RequestMapping(value = "list")
	public ModelAndView listrole(HttpServletRequest request,HttpServletResponse response,ModelMap modelMap,
			SysRole sysRole,Page<SysRole> page){
		logger.info("进角色的列表页面");
		request.setAttribute("page", sysRoleService.findByPage(sysRole, page));
		return new ModelAndView(SysRoleConstants.ROLELIST,modelMap );
	}
	/**
	 * 进新增和修改页面
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "role")
	public ModelAndView Role(String id,HttpServletRequest request,ModelMap modelMap){
		try {
			SysRole role = new SysRole();
			List<String> menuIds = null;
			if(ValidateUtils.isNotEmpty(id)){
				logger.info("进角色的修改页面----roleid"+id);
				request.setAttribute("role", sysRoleService.getById(id));
				menuIds = sysMenuService.findByRoleId(id);
			}else{
				logger.info("进角色新增页面，roleid 为空");
				role.setSortnum(sysRoleService.findMaxSortNum());
				request.setAttribute("role", role);
			}
			// 菜单信息
			JSONArray json = new JSONArray();
			List<SysMenu> menuList = sysMenuService.find(new SysMenu());
			if(menuList != null && menuList.size() > 0){
				for(SysMenu menu : menuList){
					Map<String, Object> map = new HashMap<String,Object>();
					map.put("id", menu.getUmresourceid());
					map.put("name", menu.getUmresourcename());
					map.put("pId", menu.getUmresourceparentid());
					// 选中角色拥有的菜单权限 
					if(menuIds != null && menuIds.contains(menu.getUmresourceid())){
						map.put("checked", true);
					}
					else{
						map.put("checked", false);
					}
					json.add(map);
				}
			}
			request.setAttribute("menuList", json.toString());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return new ModelAndView(SysRoleConstants.ROLE,modelMap);
	}
	/**
	 * 角色新增的方法
	 * @param sysRole
	 * @return
	 */
	@RequestMapping(value = "save" )
	@ResponseBody
	public String saveRole(SysRole sysRole,HttpServletRequest request){
		JSONObject json = new JSONObject();
		try {
			if(ValidateUtils.isNotEmpty(sysRole.getId())){
				logger.info("修改角色开始");
				sysRoleService.update(sysRole);
			}else{
				logger.info("新增角色开始");
				sysRole.setCreateaccount(request.getSession().getAttribute(SessionAttr.USERIDBYLOGIN.getValue()).toString());
//				sysRole.setSaas("234");  SAAS  wei she zhi 
				sysRoleService.add(sysRole);
			}
			json.put("result", "save_success");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			json.put("result", "save_fail");
		}
		return json.toString();
	}
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(String id){
		JSONObject json = new JSONObject();
		try {
			logger.info("删除菜单开始 ----"+id);
			sysRoleService.delete(id);
			json.put("result", "delete_success");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			json.put("result", "delete_fail");
		}
		return json.toString();
	}
}
